/**
 *  Copyright (C) 2008 Richard Hall
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the 
 *  GNU General Public License as published by the Free Software Foundation; either version 3 of the 
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along with this program; if not, 
 *  see <http://www.gnu.org/licenses>.
 *
 *  Additional permission under GNU GPL version 3 section 7
 *
 *  If you modify this Program, or any covered work, by linking or combining it with [name of library] 
 *  (or a modified version of that library), containing parts covered by the terms of 
 *  [name of library's license], the licensors of this Program grant you additional permission to 
 *  convey the resulting work. {Corresponding Source for a non-source form of such a combination shall 
 *  include the source code for the parts of [name of library] used as well as that of the 
 *  covered work.}
 *
 * 
 */

package com.gpsmedia.gui.controls;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * This class is used for displaying the pictures when they are being selected
 * for inclusion in the final output.
 * 
 * @author Richard Hall
 */
public class ImagePanel extends JPanel {

    private BufferedImage img;

    /**
	 * Simple constructor only kept in to allow it to be used with the Netbeans
	 * Swing Palette.
	 */
    public ImagePanel() {

    }
    /**
	 * Main constructor for this class.
	 * 
	 * @param image
	 *            The image to be displayed first
	 */

    public ImagePanel(String image) {
        try {
            if(img!=null){
                img.flush();
            }
            img = new BufferedImage(428, 301, BufferedImage.TYPE_INT_RGB);
            
            img = ImageIO.read(new File(image));
            
        } catch (IOException ex) {
            Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /**
	 * Changes the image being displayed.
	 * 
	 * @param image
	 *            The new image
	 */
    public void changeImage(String image) {
        try {
            img.flush();
            img = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
            img = ImageIO.read(new File(image));

            paintComponent(this.getGraphics());
        } catch (IOException ex) {
            Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

        /**
	 * Releases the memory held in img object.
         * Was put in as a work around to a memory leak which caused the application
         * to crash.
	 * 
	 */
    public void killImage(){
        img.flush();
        img = null;
    }
    
    /* (non-Javadoc)
 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
 */
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        int panelWidth = this.getWidth();
        int panelHeight = this.getHeight();
        int imageWidth = img.getWidth();
        int imageHeight = img.getHeight();

        double scale = GetScale(panelWidth, panelHeight, imageWidth, imageHeight);

        double xPos = (panelWidth - scale * imageWidth) / 2;
        double yPos = (panelHeight - scale * imageHeight) / 2;


        AffineTransform at = AffineTransform.getTranslateInstance(xPos, yPos);
        at.scale(scale, scale);

        g2.drawRenderedImage(img, at);

    }

    /**
 * Gets the scale used to resize the image.
 * 
 * @param panelWidth
 *            The width of the panel
 * @param panelHeight
 *            The height of the panel
 * @param imageWidth
 *            The width of the image
 * @param imageHeight
 *            The height of the image
 * @return The scale
 */
    private double GetScale(int panelWidth, int panelHeight, int imageWidth,
            int imageHeight) {
        double scale = 1;
        double xScale;
        double yScale;

        // should check that denom != 0 first.
        xScale = (double) panelWidth / imageWidth;
        yScale = (double) panelHeight / imageHeight;
        scale = Math.min(xScale, yScale);
        return scale;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 428, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 301, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
